//
//  DefaultAnimator.m
//  LvXi_newVersion
//
//  Created by 王敏 on 16/8/13.
//  Copyright © 2016年 SJT. All rights reserved.
//

#import "DefaultAnimator.h"

@implementation DefaultAnimator
+(instancetype)animator{
    return [[[self class] alloc] _init];
}

-(id)_init{
    if(self = [super init]){
        self.toDuration = 0.3;
        self.backDuration = 0.3;
    }
    return self;
}


-(void)xw_setToAnimation:(id<UIViewControllerContextTransitioning>)transitionContext{
    UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    UIView *containerView = [transitionContext containerView];
    [containerView addSubview:toVC.view];
    [containerView addSubview:fromVC.view];
    
    fromVC.view.x = 0;
    toVC.view.x = ScreenWidth;
    [UIView animateWithDuration:self.toDuration animations:^{
        toVC.view.x = 0;
        fromVC.view.x = - ScreenWidth;
    }completion:^(BOOL finished) {
//        [fromVC.view removeFromSuperview];
       
    }];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(self.toDuration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        CABasicAnimation * move1 =  [CABasicAnimation animationWithKeyPath:@"transform.translation.x"];
        move1.toValue = @(5);
        move1.duration = 0.1;
        move1.removedOnCompletion = NO;
        move1.fillMode = kCAFillModeForwards;
        
        CABasicAnimation * move2 =  [CABasicAnimation animationWithKeyPath:@"transform.translation.x"];
        move2.toValue = @(-10);
        move2.duration = 0.1;
        move2.removedOnCompletion = NO;
        move2.fillMode = kCAFillModeForwards;
        move2.beginTime = 0.1;
        
        CABasicAnimation * move3 =  [CABasicAnimation animationWithKeyPath:@"transform.translation.x"];
        move3.toValue = @(0);
        move3.duration = 0.1;
        
        move3.removedOnCompletion = NO;
        move3.fillMode = kCAFillModeForwards;
        move3.beginTime = 0.1;
        
        CAAnimationGroup * group = [CAAnimationGroup animation];
        group.animations = @[move1,move2,move3];
        group.removedOnCompletion = NO;
        group.fillMode = kCAFillModeForwards;
        group.duration = 0.3;
        [group setValue:transitionContext forKey:@"transitionContext"];
        [toVC.view.layer addAnimation:group forKey:nil];

    });
    
    
}

-(void)xw_setBackAnimation:(id<UIViewControllerContextTransitioning>)transitionContext{
    UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    UIView *containerView = [transitionContext containerView];
    [containerView insertSubview:toVC.view atIndex:0];
    [containerView addSubview:fromVC.view];
    fromVC.view.x = 0;
    fromVC.view.height += 20;
    toVC.view.x = -ScreenWidth * 0.25;
    fromVC.view.y = 0;
    [UIView animateWithDuration:self.toDuration animations:^{
        fromVC.view.x =  ScreenWidth;
        toVC.view.x = 0;
    }completion:^(BOOL finished) {
//        [fromVC.view removeFromSuperview];
        [containerView addSubview:toVC.view];
        [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
    }];
    
}
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
    id<UIViewControllerContextTransitioning> transitionContext = [anim valueForKey:@"transitionContext"];
    
    UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    for (id obj in toVC.view.subviews) {
        if([obj isKindOfClass:[UIImageView class]]){
            if([obj width] == ScreenWidth){
                [obj removeFromSuperview];
            }
        }
    }
    UIViewController *fromVC = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    for (id obj in fromVC.view.subviews) {
        if([obj isKindOfClass:[UIImageView class]]){
            if([obj width] == ScreenWidth){
                [obj removeFromSuperview];
            }
        }
    }
    [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
    
}

@end
